///\todo upgrade

//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef _BUOLA_ZZ_DETAIL_ENABLERS_H_
#define _BUOLA_ZZ_DETAIL_ENABLERS_H_

#include <buola/zz.h>

namespace buola { namespace zz { namespace detail {

template<typename tType,template<typename... tP> class tO>
struct MIsInstanceOf
{
    static const bool value=false;
};

template<typename... tArgs,template<typename... tP> class tO>
struct MIsInstanceOf<tO<tArgs...>,tO>
{
    static const bool value=true;
};

DECLARE_NAME(NaryExplicit);
DECLARE_NAME(NaryCombineLeft);
DECLARE_NAME(NaryCombineRight);
DECLARE_NAME(NaryCombineBoth);

template<typename tA,typename tB,template<typename... tP> class tO,typename pEnable=void>
struct MNaryConcat
{
    typedef tO<tA,tB> type;
    typedef NNaryExplicit tCombine;
};

template<typename... tA,typename tB,template<typename... tP> class tO>
struct MNaryConcat<tO<tA...>,tB,tO,typename std::enable_if<!MIsInstanceOf<tB,tO>::value,void>::type>
{
    typedef tO<tA...,tB> type;
    typedef NNaryCombineLeft tCombine;
};

template<typename tA,typename... tB,template<typename... tP> class tO>
struct MNaryConcat<tA,tO<tB...>,tO,typename std::enable_if<!MIsInstanceOf<tA,tO>::value,void>::type>
{
    typedef tO<tA,tB...> type;
    typedef NNaryCombineRight tCombine;
};

template<typename... tA,typename... tB,template<typename... tP> class tO>
struct MNaryConcat<tO<tA...>,tO<tB...>,tO,void>
{
    typedef tO<tA...,tB...> type;
    typedef NNaryCombineBoth tCombine;
};

template<typename... tA,template<typename... tP> class tO>
std::tuple<tA...> nary_extract_tuple(const tO<tA...> &pO)
{
    return pO.GetElements();
}

template<typename... tA,template<typename... tP> class tO>
std::tuple<tA...> &&nary_extract_tuple(tO<tA...> &&pO)
{
    return std::move(pO.GetElements());
}

/*namespace detail*/ } /*namespace zz*/ } /*namespace buola*/ }

#endif
